//
//  WBHomeViewController.swift
//  DLSwift微博
//
//  Created by 丁浪涛 on 2016/11/7.
//  Copyright © 2016年 丁浪涛. All rights reserved.
//

import UIKit

//定义全局常量 尽量使用Private 修饰  否则到处都可以使用
private let  cellId  = "cellId"

class WBHomeViewController: WBBaseViewController {
    //
    //    override func viewDidLoad() {
    //        super.viewDidLoad()
    //
    //        // Do any additional setup after loading the view.
    //    }
    
    //    //懒加载一个 微博数据数组
    //    fileprivate lazy var statusList = [String]()
    //列表视图模型
    fileprivate  lazy var listViewModel = WBStatusListViewModel()
    
    
    override func loadData() {
        
        listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess,hasMorePullup) in
           
            print("加载数据结束 :\(self.listViewModel.statusList.last?.text)")
            
            //结束刷新控件
            self.refreshControl?.endRefreshing()
            self.isPullup = false
            print("刷新表格")
            
            if hasMorePullup
            {
                    self.tableView?.reloadData()
            }
    
        }
        
        //        //加载数据
        //        WBNetWorkManager.shared.statusList { (json, isSuccess) in
        //            print("加载数据完成")
        //        }
        //        print("开始单例 :\(WBNetWorkManager.shared)")
        //        print("开始加载数据")
        
    }

    @objc fileprivate func  showFrinds()
    {
        print(#function)
        
        let  vc = WBDemoViewController()
        //      vc.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(vc, animated: true)
        
    }
}
//表格数据源方法
extension WBHomeViewController
{
    override  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell =  tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        return cell
        
    }
    
}
extension WBHomeViewController
{
    override func  setUpTableView() {
        super.setUpTableView()
        navItem.leftBarButtonItem =    UIBarButtonItem(tite: "好友", fontSize: 12, target: self, action: #selector(showFrinds))
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        setUpNavTitle()
    }
    
    
    fileprivate func  setUpNavTitle()
    {
        let title = WBNetWorkManager.shared.userAccount.screen_name
        let  button =  WBTitleButton(title:title)
        button.addTarget(self, action: #selector(clickTitleBtn), for: .touchUpInside)
        navItem.titleView = button
    }
    @objc func clickTitleBtn(btn: UIButton)
    {
     btn.isSelected = !btn.isSelected
    }
    
}
